package oving4;

public class player {
	static int playerX;
	static int playerY;
	static public char WALL = '#';
	static char EMPTY = ' ', TARGET = '.';
	static char PLAYER = '@', PLAYER_ON_TARGET = '+';
	static char BOX = '$', BOX_ON_TARGET = '*';
	private static int moves;
	static map m;
	player(){
		m=new map();
		playerX=getXpos();
		playerY=getYpos();
		
		
	}
	static map getMap(){
			return m;
	}
	static int getXpos(){
		for(int i=0;i<m.grid.length;i++){
			for(int j=0;j<m.s[i].length();j++){
				if(m.s[i].charAt(j)==player.PLAYER){
					return j;
				}				
			}
		}
		return -1;
	}
	static int getYpos(){
		for(int i=0;i<m.grid.length;i++){
			for(int j=0;j<m.s[i].length();j++){
				if(m.s[i].charAt(j)==player.PLAYER){
					return i;
				}				
			}
		}
		return -1;
	}
	static boolean checkMove(char retning, int verdi){
		boolean ok = true;
		if(retning == 'y'){
			 
			char c=m.grid[playerY+verdi][playerX];
			char d=m.grid[playerY+verdi+verdi][playerX];
			
			if((c==BOX||c==BOX_ON_TARGET)&&(d==WALL||d==BOX||d==BOX_ON_TARGET)){
				ok=false;
			}
			if(c==WALL){
				ok=false;
			}
		}
		else{
			char c=m.grid[playerY][playerX+verdi];
			char d=m.grid[playerY][playerX+verdi+verdi];
			if((c==BOX||c==BOX_ON_TARGET)&&(d==WALL||d==BOX||d==BOX_ON_TARGET)){
				ok=false;
			}
			if(c==WALL){
				ok=false;
				
			}
		}
		return ok;
	}
	static int getMoves(){
		return moves;
	}
	static void doMove(int dx, int dy){
		moves++;
		if(m.grid[playerY][playerX]==PLAYER_ON_TARGET){
			m.grid[playerY][playerX]=TARGET;
		}
		else{
			m.grid[playerY][playerX]=EMPTY;
		}
		playerY+=dy;
		playerX+=dx;
		if(m.grid[playerY][playerX]==BOX){
			m.grid[playerY][playerX]=PLAYER;
			if(m.grid[playerY+dy][playerX+dx]==TARGET){
				m.grid[playerY+dy][playerX+dx]=BOX_ON_TARGET;
			}
			else{
				m.grid[playerY+dy][playerX+dx]=BOX;
			}
		}
		else if(m.grid[playerY][playerX]==BOX_ON_TARGET){
			m.grid[playerY][playerX]=PLAYER_ON_TARGET;
			m.grid[playerY+dy][playerX+dx]=BOX;
		}
		else if(m.grid[playerY][playerX]==TARGET){
			m.grid[playerY][playerX]=PLAYER_ON_TARGET;
			
		}
		else{
			m.grid[playerY][playerX]=PLAYER;
		}
		
	}
	
	
}
